package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Keyword;
import com.cskaoyan.bean.pojo.KeywordExample;
import com.cskaoyan.bean.pojo.SearchHistory;
import com.cskaoyan.bean.wx.search.vo.DefaultVo;
import com.cskaoyan.bean.wx.search.vo.HistoryVo;
import com.cskaoyan.bean.wx.search.vo.HotVo;
import com.cskaoyan.bean.wx.search.vo.IndexVo;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.wx.WxSearchService;
import com.cskaoyan.util.CurrentUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Name : WxSearchServiceImpl.java
 * @Time : 2021/8/16 20:10
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    UserMapper userMapper;


    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Override
    public IndexVo getSearchIndex() {
        IndexVo indexVo = new IndexVo();
        List<HotVo> hotVos = new ArrayList<>();
        List<HistoryVo> historyVos = new ArrayList<>();
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsDefaultEqualTo(true);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        DefaultVo defaultVo = selectDefault(keywords);
        List<Keyword> hotKeyword = keywordMapper.selectHot();
        for (Keyword keyword : hotKeyword) {
            HotVo hotVo = transferHot(keyword);
            hotVos.add(hotVo);
        }
        Integer currentUserId = CurrentUserUtils.getCurrentUserId(userMapper);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectBytime(currentUserId);
        for (SearchHistory searchHistory : searchHistories) {
            HistoryVo historyVo = transferHistory(searchHistory);
            historyVos.add(historyVo);
        }
        indexVo.setDefaultKeyword(defaultVo);
        indexVo.setHotKeywordList(hotVos);
        indexVo.setHistoryKeywordList(historyVos);
        return indexVo;
    }

    @Override
    public List<String> getSearchContent(String keyword) {
        Integer currentUserId = CurrentUserUtils.getCurrentUserId(userMapper);
        List<String> history = searchHistoryMapper.selectByKeyword(keyword, currentUserId);
        return history;
    }

    @Override
    public void clearHistory() {
        Integer currentUserId = CurrentUserUtils.getCurrentUserId(userMapper);
        searchHistoryMapper.updateByUserIdWithDelete(currentUserId);
    }

    private HistoryVo transferHistory(SearchHistory searchHistory) {
        HistoryVo historyVo = new HistoryVo();
        historyVo.setAddTime(searchHistory.getAddTime());
        historyVo.setDeleted(searchHistory.getDeleted());
        historyVo.setFrom(searchHistory.getFrom());
        historyVo.setId(searchHistory.getId());
        historyVo.setKeyword(searchHistory.getKeyword());
        historyVo.setUpdateTime(searchHistory.getUpdateTime());
        historyVo.setUserId(searchHistory.getUserId());
        return historyVo;
    }

    private HotVo transferHot(Keyword keyword) {
        HotVo hotVo = new HotVo();
        hotVo.setAddTime(keyword.getAddTime());
        hotVo.setDeleted(keyword.getDeleted());
        hotVo.setId(keyword.getId());
        hotVo.setIsDefault(keyword.getIsDefault());
        hotVo.setIsHot(keyword.getIsHot());
        hotVo.setKeyword(keyword.getKeyword());
        hotVo.setSortOrder(keyword.getSortOrder());
        hotVo.setUpdateTime(keyword.getUpdateTime());
        hotVo.setUrl(keyword.getUrl());
        return hotVo;
    }

    private DefaultVo selectDefault(List<Keyword> keywords) {
        DefaultVo defaultVo = new DefaultVo();
        defaultVo.setId(keywords.get(0).getId());
        defaultVo.setKeyword(keywords.get(0).getKeyword());
        defaultVo.setUpdateTime(keywords.get(0).getUpdateTime());
        defaultVo.setUrl(keywords.get(0).getUrl());
        defaultVo.setIsHot(keywords.get(0).getIsHot());
        defaultVo.setIsDefault(keywords.get(0).getIsDefault());
        defaultVo.setSortOrder(keywords.get(0).getSortOrder());
        defaultVo.setAddTime(keywords.get(0).getAddTime());
        defaultVo.setDeleted(keywords.get(0).getDeleted());
        return defaultVo;
    }
}
